import requests
from lxml import etree


class ChkUpd:

    def __init__(self):
        """
        更新模块
        """

        self.headers = {
            "Host": "gitee.com",
            "Connection": "keep-alive",
            "Cache-Control": "max-age=0",
            "Upgrade-Insecure-Requests": 1,
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
            "Referer": "https://gitee.com/creeper2007/roxy-software-r/edit/master/README.md",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "zh-CN,zh;q=0.9",
        }
        self.url = "https://gitee.com/creeper2007/roxy-software-r/blob/master/README.md"

    def chk_new_version(self):
        """
        检查最新版本
        :return:
        """
        try:
            r = requests.get(url=self.url, headers=self.headers, verify=False).text
            tree = etree.HTML(r)
            tree_res = tree.xpath("//*[@id=\"tree-content-holder\"]/div/div[4]/h3/text()")
            # reslist.append({"name": res[0].text, "url": "http://m.06qb.com" + res[0].attrib['href']})
            if tree_res[1].split(":")[0] == "Now Edition":
                res = tree_res[1].split(":")[1]
            else:
                print("Gitee Readme File 格式出错")
                return "x"
            return res
        except Exception as e:
            print("Upd Error: ", e)
            return "x"


if __name__ == "__main__":
    check = ChkUpd()
    print(check.chk_new_version())
